package com.qzw.security.web.controller;

import com.qzw.common.web.model.NavMenu;
import com.qzw.common.web.model.Result;
import com.qzw.common.web.util.ResultUtils;
import com.qzw.security.api.model.Menu;
import com.qzw.security.api.model.User;
import com.qzw.security.api.service.IMenuService;
import com.qzw.security.api.service.IUserService;
import com.qzw.security.spring.config.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

/**
 * @author ：quziwei
 * @date ：Created in 2020/6/8 10:43 下午
 * @description：权限验证
 */
@RestController
@RequestMapping("/auth")
@Api(tags = "权限验证")
public class AuthController {
    @Autowired
    private IMenuService menuService;
    @Autowired
    private IUserService userService;



    @GetMapping("/navigation")
    @ApiOperation("获取用户导航栏配置")
    public Set<NavMenu> navigation() {
        List<Menu> menus = menuService.findMenusConfigByUserId(SecurityUtils.getUserId());

        NavMenu<Menu> navMenu = new NavMenu();
        Menu menu = menuService.findById(1);
        navMenu.setValue(menu);
        findMenuChildren(menus, navMenu);

        return navMenu.getChildren();
    }

    @GetMapping("/userInfo")
    @ApiOperation("获取用户信息")
    public User userInfo() {
        User user = userService.findById(SecurityUtils.getUserId());
        user.setPassword(null);

        return user;
    }


    @GetMapping("/login")
    @ApiOperation("登录页面")
    public Result loginPage() {
        Result result = ResultUtils.result(Result.Code.NO_LOGIN.getValue(), null);
        return null;
    }

    @PostMapping("/login")
    @ApiOperation("登录")
    public void login(String username, String password) {
        return;
    }

    @GetMapping("/loginFail")
    @ApiOperation("登录失败")
    public Result loginFail() {
        return ResultUtils.result(Result.Code.FAIL.getValue(), "用户名或密码错误");
    }

    @PostMapping("/loginSuccess")
    @ApiOperation("登录成功")
    public Result loginSuccess() {
        return ResultUtils.result();
    }

    @GetMapping("/logout")
    @ApiOperation("退出")
    public void logout() {
        return;
    }

    /**
     * 格式化导航栏返回值
     *
     * @param menus
     * @param navMenu
     */
    private void findMenuChildren(List<Menu> menus, NavMenu<Menu> navMenu) {
        Integer parentId = navMenu.getValue().getId();
        for (Menu menu : menus) {
            if (parentId.equals(menu.getParentId())) {
                NavMenu<Menu> n = new NavMenu();
                n.setValue(menu);
                boolean result = navMenu.getChildren().add(n);
                if (result) {
                    findMenuChildren(menus, n);
                }
            }
        }
    }
}
